package com.glela.packet.service.impl;

import com.glela.common.util.DateUtil;
import com.glela.packet.mapper.RedPacketBizMapper;
import com.glela.packet.mapper.RedPacketMapper;
import com.glela.packet.model.RedPacket;
import com.glela.packet.service.RedPacketService;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("redPacketService")
public class RedPacketServiceImpl implements RedPacketService {

  @Autowired
  private RedPacketBizMapper redPacketBizMapper;
  @Autowired
  private RedPacketMapper redPacketMapper;
  
  @Override
  public boolean batchSaveRedPacket(List<RedPacket> redPacketList) {
    return redPacketBizMapper.batchInsert(redPacketList) > 0;
  }

  @Override
  public RedPacket getRedPacketById(int id) {
    return redPacketMapper.selectByPrimaryKey(id);
  }

  @Override
  public BigDecimal queryTotalRedPacketByUserId(long userId) {
	  Date nowDate = DateUtil.parse(DateUtil.getNow());
	  BigDecimal reult = redPacketBizMapper.queryTotalRedPacketByUserId(userId,nowDate);
	  if(null == reult){
		  reult = BigDecimal.ZERO; 
	 }
	  return reult;
  }

  @Override
  public boolean updateRedPacket(RedPacket redPacket) {
    return redPacketMapper.updateByPrimaryKeySelective(redPacket) > 0;
  }


  @Override
  public boolean saveRedPacket(RedPacket redPacket) {
    return redPacketMapper.insert(redPacket) > 0;
  }

  @Override
  public boolean updateRedPacketAll(RedPacket redPacket) {
    return redPacketMapper.updateByPrimaryKey(redPacket) > 0;
  }
}
